接下來將[day-20] K-means 分類法分類影像 分類法串接上去。
我們將k-means程式改寫為
## KMEANFunc.py
import skimage
from sklearn import cluster
import numpy
def image_kmeans_classification(image_array):
image_array = numpy.asarray(image_array)
#print(image_array)
row_number = image_array.shape[0]
col_number = image_array.shape[1]
sample = image_array.reshape((row_number*col_number,3))
#print(sample)
kmeans_classifer = cluster.KMeans(n_clusters = 4,random_state=0).fit(sample)
kmeans_result = kmeans_classifer.predict(sample)
kmeans_result = (kmeans_result-numpy.min(kmeans_result))/(numpy.max(kmeans_result)-numpy.min(kmeans_result))*255
image_array_classification = kmeans_result.reshape((row_number,col_number))
return image_array_classification
接下來我們將我們k-means串接上去
from ODMFunc import Run_Nodeodm, Catch_Filename
from IMAGEFunc import std_convoluted, add_exif
from KMEANFunc import image_kmeans_classification
def main():
sys.path.append('..')
upload_file = sys.argv[1] # 將外部參數sys.avgv[1]作為upload檔案輸入。
filename_list = Catch_Filename(upload_file) # 遍歷upload_file的所有Jpg並且回傳一個list
Run_Nodeodm(filename_list) # 執行影像建模
img_path = "./results/odm_orthophoto/odm_orthoimage.tif"
img_output_path = "./results/odm_orthoimage_variance.tif"
kmeans_image_path = "./results/odm_orthoimage_kmeans.tif"
img = skimage.io.imread(img_path)
img_g = img[:,:,1]
N = 3
img_var = std_convoluted(img_g, N)
img_var = numpy.asarray(img_var)
skimage.io.imsave(img_output_path,img_var)
add_exif(img_output_path,img_path)
# kmeans classification
image_array = skimage.io.imread(img_output_path)
image_array_classification = image_kmeans_classification(image_array)
skimage.io.imsave(img_output_path,kmeans_image_path)
接下來就剩下將計算數量的串聯上去了。